#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>

#define SYSCALL_GET_222 222
#define SYSCALL_PUT_223 223
#define SYSCALL_DEL_251 251

struct test_data {
   char *value;
   size_t vsize;
   char *key;
   size_t ksize;
};

int main(int argc, char * argv [ ]){
	if(argc != 2){
		fprintf(stdout, "Usage: %s <times>\n", argv[0]);
		exit(-1);
	}
	
	char key[100];
	char value[100];
	
	struct test_data data;	
	data.key = key;
	data.value = value;

	unsigned long cnt;
	unsigned long i = strtoul(argv[1], NULL, 10);

	/* start timer */
#ifdef MS_TEST
	/* calculate runtime in us */
	struct timeval start, end;
	unsigned long timer;
	gettimeofday(&start,NULL);
#else
	/* caculate runtime in ns */
	struct timespec time_start={0, 0}, time_end={0, 0};
	clock_gettime(CLOCK_REALTIME, &time_start);
#endif

	for(cnt = 0; cnt < i; cnt++){
		sprintf(key, "%lu", cnt);	

		data.ksize = strlen(key) + 1;
		
		syscall(SYSCALL_DEL_251, data.key, data.ksize);	
	}

	/* stop timer */
#ifdef MS_TEST
	gettimeofday(&end,NULL);
	timer = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec;
	printf("timer = %ld us\n",timer);
#else		
	clock_gettime(CLOCK_REALTIME, &time_end);
	//printf("statr&end(ns): %lu & %lu\n", time_start.tv_nsec, time_end.tv_nsec);
	if(time_end.tv_nsec < time_start.tv_nsec){
		printf("duration:%lus %luns\n", time_end.tv_sec - time_start.tv_sec - 1, 
					1000000000 + time_end.tv_nsec - time_start.tv_nsec);
	}
	else{
		printf("duration:%lus %luns\n", time_end.tv_sec - time_start.tv_sec, 
						time_end.tv_nsec - time_start.tv_nsec);
	}
#endif

	return 0;
}


